<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'rd:rd10:m8Rd10:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsM8RdRd10M8Rd10List">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { m8Rd10Delete, m8Rd10ListData } from '/@/api/m8/rd/rd10/m8Rd10';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  const { t } = useI18n('m8.rd.rd10.m8Rd10');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('产品入库单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('入库单号'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('单据日期'),
        field: 'ddate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
          valueFormat: 'YYYY-MM-DD',
        },
      },
      // {
      //   label: t('业务类型'),
      //   field: 'busType',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'sc_bus_type',
      //     allowClear: true,
      //   },
      // },
      
      {
        label: t('单据状态'),
        field: 'djStatus',
        component: 'Select',
        componentProps: {
          dictType: 'dj_status',
          allowClear: true,
        },
      },
      {
        label: t('单据来源'),
        field: 'csource',
        component: 'Select',
        componentProps: {
          dictType: 'm8_rd10_source',
          allowClear: true,
        },
      },
    ],
    fieldMapToTime: [['ddate', ['ddate_gte', 'ddate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
  {
      title: t('入库单号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 110,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('单据日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 90,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('制单时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 100,
      align: 'left',
      // slot: 'firstColumn',
    },
    // {
    //   title: t('业务类型'),
    //   dataIndex: 'busType',
    //   key: 'a.bus_type',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'sc_bus_type',
    // },
    {
      title: t('制单人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('修改时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('修改人'),
      dataIndex: 'updateByName',
      key: 'a.update_by_name',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 140,
      align: 'left',
    },
    {
      title: t('单据状态'),
      dataIndex: 'djStatus',
      key: 'a.dj_status',
      sorter: true,
      width: 90,
      align: 'center',
      dictType: 'dj_status',
      fixed: 'left',
    },
    {
      title: t('单据来源'),
      dataIndex: 'csource',
      key: 'a.csource',
      sorter: true,
      width: 90,
      align: 'center',
      dictType: 'm8_rd10_source',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑产品入库单'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'rd:rd10:m8Rd10:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除产品入库单'),
        popConfirm: {
          title: t('是否确认删除产品入库单'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'rd:rd10:m8Rd10:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: m8Rd10ListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { code: record.code };
    const res = await m8Rd10Delete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
